package view;

import Dao.UserDao;
import Model.Obj.SonSume;

import java.awt.EventQueue;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout.Alignment;
import javax.swing.table.DefaultTableModel;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.List;
import java.util.Vector;

public class SonsumeGUI extends JFrame {

    private JPanel contentPane;
    private JTable table;
    private JTextField textField;
    private JTextField textField_1;
    private DefaultTableModel dtm;
    private JTextField textField_2;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SonsumeGUI frame = new SonsumeGUI();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public SonsumeGUI() {
        setTitle("\u6D88\u8D39\u7BA1\u7406");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 700, 400);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);

        JScrollPane scrollPane = new JScrollPane();

        JButton btnNewButton = new JButton("\u67E5\u8BE2\u6D88\u8D39\u8BB0\u5F55");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btnNewButtonActionPerformed(e);
            }
        });

        JLabel lblNewLabel = new JLabel("\u4F1A\u5458\u5361\u53F7");

        textField = new JTextField();
        textField.setColumns(10);

        JButton btnNewButton_1 = new JButton("\u67E5\u8BE2\u6240\u6709\u6D88\u8D39\u8BB0\u5F55");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btnNewButton_1ActionPerformed(e);
            }
        });

        JLabel lblNewLabel_1 = new JLabel("\u6D88\u8D39\u65F6\u95F4\u6BB5");

        textField_1 = new JTextField();
        textField_1.setColumns(10);

        textField_2 = new JTextField();
        textField_2.setColumns(10);
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
                gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addGap(34)
                                                .addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
                                                        .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 396, GroupLayout.PREFERRED_SIZE)
                                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                                                        .addComponent(lblNewLabel_1)
                                                                        .addComponent(lblNewLabel))
                                                                .addGap(18)
                                                                .addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
                                                                        .addComponent(textField, GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE)
                                                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                                                .addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                                                .addGap(18)
                                                                                .addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                                                                .addPreferredGap(ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
                                                                .addComponent(btnNewButton)
                                                                .addGap(24))))
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addGap(156)
                                                .addComponent(btnNewButton_1)))
                                .addContainerGap(44, Short.MAX_VALUE))
        );
        gl_contentPane.setVerticalGroup(
                gl_contentPane.createParallelGroup(Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 190, GroupLayout.PREFERRED_SIZE)
                                .addGap(18)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel)
                                        .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(18)
                                .addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel_1)
                                        .addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(btnNewButton)
                                        .addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                                .addComponent(btnNewButton_1)
                                .addContainerGap())
        );
        table = new JTable();
        String[][] data = new String[][]{};
        String[] column ={"会员卡号","消费金额","存款金额","结存余额","消费时间"};
        dtm = new DefaultTableModel(data,column){
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        table.setModel(dtm);
        scrollPane.setViewportView(table);
        contentPane.setLayout(gl_contentPane);
    }
    //查询所有消费记录
    private void btnNewButton_1ActionPerformed(ActionEvent e) {
        List<SonSume> list = UserDao.selectALLSonsume();
        for (SonSume sonSume : list){
            Vector v = new Vector();
            v.add(sonSume.getCARDNO());
            v.add(sonSume.getSPENT());
            v.add(sonSume.getSAVE());
            v.add(sonSume.getBALANCE());
            v.add(sonSume.getTIME());
            dtm.addRow(v);
        }
    }
    //查询消费记录
    private void btnNewButtonActionPerformed(ActionEvent e) {
        String CardID = textField.getText();
        String TIME1 = textField_1.getText();
        String TIME2 = textField_2.getText();
        List<SonSume> list = null;
        if (CardID.isEmpty()) {
            JOptionPane.showMessageDialog(null, "会员卡号为空");
            return;
        }
        if (!CardID.isEmpty() && TIME1.isEmpty() ){
            list = UserDao.selectSonsume(CardID);
        } else {
            list = UserDao.selectSonsume(CardID,TIME1,TIME2);
        }
        for (SonSume sonSume : list){
            Vector v = new Vector();
            v.add(sonSume.getCARDNO());
            v.add(sonSume.getSPENT());
            v.add(sonSume.getSAVE());
            v.add(sonSume.getBALANCE());
            v.add(sonSume.getTIME());
            dtm.addRow(v);
        }
    }
}
